<html>
 <head>
  <meta charset="UTF-8">
 </head>
 <body>
  <p data-lake-id="u5b50e480" id="u5b50e480"><br></p>
  <h1 data-lake-id="elH3c" id="elH3c"><span data-lake-id="u9a852716" id="u9a852716">典型回答</span></h1>
  <p data-lake-id="u50f134a7" id="u50f134a7"><br></p>
  <p data-lake-id="uc3e65e30" id="uc3e65e30"><span data-lake-id="ufa600a5e" id="ufa600a5e">在IDEA中有一个设置，那就是当我们在断点处单击鼠标右键就会弹出一个设置对话框，当我们把其中的All 修改为 Thread之后，尝试重新执行debug代码，每一个线程都会进入到断点当中了。就实现了对多线程进行debug</span></p>
  <h1 data-lake-id="XmSsW" id="XmSsW"><span data-lake-id="ud73082b2" id="ud73082b2">扩展知识</span></h1>
  <h2 data-lake-id="a2JNh" id="a2JNh"><span data-lake-id="u4457b8c1" id="u4457b8c1">多线程Debug示例</span></h2>
  <p data-lake-id="u7400b2e5" id="u7400b2e5"><br></p>
  <p data-lake-id="u8712df7a" id="u8712df7a"><span data-lake-id="u78abd96e" id="u78abd96e">首先我们写一个多线程的例子，使用继承Runnable接口的方式定义多个线程，并启动执行。</span></p>
  <p data-lake-id="u2834117c" id="u2834117c"><br></p>
  <pre lang="java"><code>
/**
 * @author Hollis
 */
public class MultiThreadDebug {

    public static void main(String[] args) {
        MyThread myThread = new MyThread();

        Thread thread1 = new Thread(myThread, "thread 1");
        Thread thread2 = new Thread(myThread, "thread 2");
        Thread thread3 = new Thread(myThread, "thread 3");

        thread1.start();

        thread2.start();

        thread3.start();
    }
}

class MyThread implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " running");
    }
}

我们尝试在代码中设置断点，并使用debug模式启动。
</code></pre>
  <p data-lake-id="ufc9be8a5" id="ufc9be8a5"><br></p>
  <p data-lake-id="uc4d808f3" id="uc4d808f3"><img src="http://www.hollischuang.com/wp-content/uploads/2020/11/16065562943648.jpg?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_31%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"><span data-lake-id="u37fd4971" id="u37fd4971">​</span></p>
  <p data-lake-id="u86f8c065" id="u86f8c065"><br></p>
  <p data-lake-id="u98444fc3" id="u98444fc3"><span data-lake-id="ue102539d" id="ue102539d">如题，程序启动后，会进入一个线程的断点中，我们尝试看一下当前是哪个线程：</span></p>
  <p data-lake-id="u039db381" id="u039db381"><br></p>
  <p data-lake-id="u74445930" id="u74445930"><img src="http://www.hollischuang.com/wp-content/uploads/2020/11/16065563249582.jpg?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_16%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"><span data-lake-id="ue5ec6fdb" id="ue5ec6fdb">​</span></p>
  <p data-lake-id="u4e2b0a40" id="u4e2b0a40"><br></p>
  <p data-lake-id="uc344ca7a" id="uc344ca7a"><span data-lake-id="u666cf04d" id="u666cf04d">发现是thread 1进入了断点。接着，我们尝试让代码继续执行，代码就直接结束运行，并且控制台打印如下：</span></p>
  <p data-lake-id="u0152fb50" id="u0152fb50"><br></p>
  <pre lang="java"><code>
Connected to the target VM, address: '127.0.0.1:55768', transport: 'socket'
thread 3 running
Disconnected from the target VM, address: '127.0.0.1:55768', transport: 'socket'
thread 2 running
thread 1 running

Process finished with exit code 0
</code></pre>
  <p data-lake-id="u8bc5666f" id="u8bc5666f"><br></p>
  <p data-lake-id="ub383d468" id="ub383d468"><span data-lake-id="u4ede1d6d" id="u4ede1d6d">如果我们多次执行这个代码，就会发现，每一次打印的结果都不一样，三个线程的输出顺序是随机的，并且每一次debug只会进入到一个线程的执行。</span></p>
  <p data-lake-id="ucb72d736" id="ucb72d736"><br></p>
  <p data-lake-id="u8ed1e548" id="u8ed1e548"><span data-lake-id="uf0a4f1a6" id="uf0a4f1a6">每次执行结果随机是因为不一定哪个线程可以先获得CPU时间片。</span></p>
  <p data-lake-id="u9ee95094" id="u9ee95094"><br></p>
  <p data-lake-id="ub6f47386" id="ub6f47386"><span data-lake-id="u6e0bf577" id="u6e0bf577">那么，我们怎么才能让每一个线程的执行都能被debug呢？如何在多线程中进行debug排查问题呢？</span></p>
  <p data-lake-id="ue3dc1bc9" id="ue3dc1bc9"><br></p>
  <p data-lake-id="uffeda708" id="uffeda708"><span data-lake-id="u8d0734f1" id="u8d0734f1">其实，在IDEA中有一个设置，那就是当我们在断点处单击鼠标右键就会弹出一个设置对话框，当我们把其中的All 修改为 Thread之后，尝试重新执行debug代码。</span></p>
  <p data-lake-id="u229b7948" id="u229b7948"><br></p>
  <p data-lake-id="u69942dd7" id="u69942dd7"><img src="http://www.hollischuang.com/wp-content/uploads/2020/11/16065565440571.jpg?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_17%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"><span data-lake-id="u2eb09f82" id="u2eb09f82">​</span></p>
  <p data-lake-id="ubeb4370d" id="ubeb4370d"><br></p>
  <p data-lake-id="ud59d2dd3" id="ud59d2dd3"><span data-lake-id="u0440d187" id="u0440d187">重新执行之后，就可以发现，每一个线程都会进入到断点当中了。</span></p>
 </body>
</html>